package com.woniu.zsd.permission.controller;


import com.woniu.form.AddRoleForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.permission.Permission.Permission;
import com.woniu.zsd.permission.entity.Roles;
import com.woniu.zsd.permission.service.IRolesService;
import com.woniu.zsd.permission.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "管理员权限管理")
public class RolesController {
    @Autowired
    private IRolesService rolesService;

    @Autowired
    private TokenUtil tokenUtil;

    @PostMapping("/updateRole")
    @ApiOperation(value = "修改用户角色")
    public Result updateRole(@ApiParam(name = "adminId", value = "管理员ID")@RequestParam Integer adminId, @ApiParam(name = "role", value = "角色ID")@RequestParam Integer role, @RequestHeader String token) {
        Integer adminRole = tokenUtil.getRole(token);
        rolesService.updateRole(adminId, role, adminRole);
        return Result.getResult().setCode(200);
    }
    @GetMapping("/selAllRole")
    @ApiOperation(value = "查询全部用户角色")
    public Result<List<Roles>> selAllRole(@RequestHeader String token) {
        List<Roles> roles = rolesService.selAllRole();
        return new Result<List<Roles>>().setData(roles).setCode(200).setMsg("查询成功");
    }


    @PostMapping("/insertRole")
    @ApiOperation(value = "添加用户角色")
    @Permission("insertRole")
    public Result insertRole(@ApiParam(name = "role", value = "角色ID")@RequestBody AddRoleForm arf, @RequestHeader String token) {
        Integer adminId = tokenUtil.getAdminId(token);
        rolesService.insertRole(arf, adminId);
        return Result.getResult().setCode(200);
    }

    @DeleteMapping("/deleteRole")
    @ApiOperation(value = "删除用户角色")
    @Permission("deleteRole")
    public Result deleteRole(@ApiParam(name = "roleId", value = "角色ID")@RequestParam Integer roleId) {
        rolesService.removeById(roleId);
        return Result.getResult().setCode(200);
    }
}
